using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
namespace Lcygcc.Infrastructure.WindowService
{
	public class WindowsServiceTool
	{
		private string installCommand = "sc create {0} binpath= \"{1}\" displayName= \"{0}\"   start= auto  " + Environment.NewLine + " sc  description {0} \"{2}\"";
		private string uninstallCommand = "net stop {0}\r\n                                                    sc stop {0}\r\n                                                    sc delete {0}\r\n                                                    ";
		private string _startCommand = "net start {0}";
		private string _stopCommand = "net stop {0}";
	//	private ILogFactory iLogFactory = new LogFactory();
		public static readonly WindowsServiceTool Instance = new WindowsServiceTool();
		private WindowsServiceTool()
		{
		}
		public bool ServiceIsExist(string serviceName)
		{
			ServiceController[] services = ServiceController.GetServices();
			ServiceController[] array = services;
			for (int i = 0; i < array.Length; i++)
			{
				ServiceController serviceController = array[i];
				if (serviceController.ServiceName == serviceName)
				{
					return true;
				}
			}
			return false;
		}
		public bool ServiceIsRunning(string serviceName)
		{
			ServiceController[] services = ServiceController.GetServices();
			ServiceController serviceController = services.FirstOrDefault((ServiceController x) => x.ServiceName == serviceName);
			return serviceController != null && serviceController.Status == ServiceControllerStatus.Running;
		}
		public bool UninstallService(string serviceName, string path)
		{
			if (this.ServiceIsExist(serviceName) && this.ServiceIsExist(serviceName))
			{
				string commandString = string.Format(this.uninstallCommand, serviceName);
				this.Excute(commandString, path);
				return true;
			}
			return false;
		}
		public bool InstallService(string serviceName, string path, string description)
		{
			if (!this.ServiceIsExist(serviceName))
			{
				string commandString = string.Format(this.installCommand, serviceName, path, description);
				this.Excute(commandString, path);
				return true;
			}
			return false;
		}
		public bool StartService(string serviceName, string path)
		{
			if (this.ServiceIsExist(serviceName))
			{
				string commandString = string.Format(this._startCommand, serviceName);
				this.Excute(commandString, path);
				return true;
			}
			return false;
		}
		public bool StopService(string serviceName, string path)
		{
			if (this.ServiceIsExist(serviceName))
			{
				string commandString = string.Format(this._stopCommand, serviceName);
				this.Excute(commandString, path);
				return true;
			}
			return false;
		}
		private void Excute(string commandString, string path)
		{
			//ILogger logger = this.iLogFactory.CreateLogger("服务安装卸载");
			try
			{
				Process process = new Process();
				process.StartInfo = new ProcessStartInfo();
				process.StartInfo.FileName = "cmd.exe";
				process.StartInfo.CreateNoWindow = true;
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.RedirectStandardInput = true;
				process.StartInfo.RedirectStandardOutput = true;
				process.Start();
				process.BeginOutputReadLine();
				process.StandardInput.WriteLine("cd\\");
				process.StandardInput.WriteLine(@"C:");
				process.StandardInput.WriteLine("cd " + path.Substring(0, path.LastIndexOf("\\") + 1));
				process.StandardInput.WriteLine(commandString);
			}
			catch (Exception ex)
			{
	//			logger.Error("服务", "操作出错", ex);
			}
			finally
			{
	//			logger.Complete();
			}
		}
	}
}
